package com.meiyu.live.ui.mine;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.WithdrawResponse;
import com.meiyu.live.popj.WithdrawInfo;
import com.meiyu.live.util.RippleUtils;
import com.meiyu.live.widget.MSGView;

import java.text.SimpleDateFormat;

/**
 * 提现记录
 */
public class WithdrawActivity extends BaseActivity implements View.OnClickListener {


    private LayoutInflater inflater;
    private MSGView msgView;
    private SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private DataListAdapter adapter;
    private View btn_back, footerView;

    private static final int PAGE_SIZE = 12;
    private int index = 1;
    private boolean hasMore = true;
    private WithdrawResponse withdrawResponse = new WithdrawResponse();
    private SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw);
        init();
    }

    protected void init() {
        inflater = LayoutInflater.from(this);
        msgView = (MSGView) findViewById(R.id.msgView);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);

        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
        listView = (ListView) findViewById(R.id.listview);

        footerView = inflater.inflate(R.layout.view_loadmore, null);
        listView.addFooterView(footerView);
        footerView.setVisibility(View.GONE);

        adapter = new DataListAdapter();
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { // 判断滚动到底部
                    if (absListView.getLastVisiblePosition() == (absListView.getCount() - 1) && hasMore) {
                        footerView.setVisibility(View.VISIBLE);
                        getData(false);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView absListView, int i, int i1, int i2) {

            }
        });

        getData(true);
    }

    private void getData(final boolean reload) {
        msgView.showLoading();
        //热门直播接口
        if (reload) {
            index = 1;
            hasMore = true;
        }
        int area[] = {PAGE_SIZE, index + PAGE_SIZE - 1};
        BaseBodyParams gp = new BaseBodyParams();
        gp.setUrl(Constant.UrlParams.WITHDRAW_LOG);
        request(gp, WithdrawResponse.class, new Response.Listener<WithdrawResponse>() {
            @Override
            public void onResponse(WithdrawResponse response) {
                refreshLayout.setRefreshing(false);
                footerView.setVisibility(View.GONE);
                if (withdrawResponse != null) {
                    if (reload) {
                        withdrawResponse.getHash().clear();
                    }
                    withdrawResponse.getHash().addAll(response.getHash());
                    index += PAGE_SIZE;
                    if (withdrawResponse.getHash().size() < PAGE_SIZE) {
                        hasMore = false;
                    }
                    adapter.notifyDataSetChanged();
                    if (withdrawResponse.getHash().size() == 0) {
                        msgView.show_empty();
                    } else {
                        msgView.dismiss();
                    }
                } else {
                    msgView.show_error();
                }
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onError() {
        super.onError();
        refreshLayout.setRefreshing(false);
        footerView.setVisibility(View.GONE);
        msgView.show_error();
    }

    @Override
    public void onClick(View view) {
        if (view == btn_back) {
            finish();
        }
    }

    class DataListAdapter extends BaseAdapter {

        private class ViewHolder {
            View layout_root;
            TextView tv_note, tv_money, tv_create_time;
        }

        int mainColor = getResources().getColor(R.color.main_theme_color);

        @Override
        public int getCount() {
            return withdrawResponse != null && withdrawResponse.getHash() != null ? withdrawResponse.getHash().size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.adapter_withdraw, null);
                holder = new ViewHolder();
                holder.layout_root = convertView.findViewById(R.id.layout_root);
                RippleUtils.setRippleDrawable(holder.layout_root, mainColor);
                holder.tv_note = (TextView) convertView.findViewById(R.id.tv_note);
                holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                holder.tv_create_time = (TextView) convertView.findViewById(R.id.tv_create_time);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.layout_root.setTag(position);
            final WithdrawInfo bean = withdrawResponse.getHash().get(position);
            String msg = "无效";
            switch (bean.getStatus()) {
                case 0:
                    msg = "提现审核中";
                    break;
                case 1:
                    msg = "提现成功";
                    break;
            }
            holder.tv_note.setText(msg);
            holder.tv_money.setText("本次提现金额" + bean.getMoney()/100 + "元");
            holder.tv_create_time.setText(format.format(bean.getCreate_at() * 1000));
            return convertView;
        }
    }
}
